//
//  DownloadMutilFiles.m
//  DownloadTest
//
//  Created by Bad on 2018/5/18.
//  Copyright © 2018年 boxian. All rights reserved.
//

#import "DownloadMutilFiles.h"
#import "DownLoadUtils.h"

@implementation DownloadMutilFiles

+ (void)downloadForUrls:(NSArray *)urls
              fileNames:(NSArray *)fileNames
            toFilePaths:(NSArray *)toFilePaths
      downloadFailRetry:(BOOL)retry
               progress:(void (^)(double downloadProgress))progress
      completionHandler:(void (^)(BOOL allSuccess, NSArray *errorUrls, NSArray *errors))completionHandler  {
    
    if (!urls || urls.count == 0) {
        NSLog(@"url为空");
        return;
    }
    if (!toFilePaths || toFilePaths.count == 0) {
        NSLog(@"toFilePaths为空");
        return;
    }
    NSMutableArray *tempErrorURLs = [NSMutableArray array];
    NSMutableArray *tempErrors = [NSMutableArray array];
    
    NSMutableArray *tempErrorFileNames = [NSMutableArray array];
    NSMutableArray *tempErrorFilePaths = [NSMutableArray array];
    
    __block NSInteger successCount = 0;
    __block NSInteger failureCount = 0;
    NSInteger count = urls.count;
    NSInteger fileNameCount = fileNames.count;
    NSString *fileName = nil;
    NSString *tempFilePath = toFilePaths[0];
    
    for (NSInteger i = 0; i < count; i++) {
        NSString *url = urls[i];
        if (toFilePaths.count > i) {
            tempFilePath = toFilePaths[i];
        }
        if (fileNameCount > i) {
            fileName = fileNames[i];
        }
        [DownLoadUtils downloadWithURL:url toFilePath:tempFilePath fileName:fileName progress:^(NSProgress *downloadProgress) {
            
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            if (error) {
                failureCount++;
                [tempErrorURLs addObject:url];
                [tempErrors addObject:error];
                [tempErrorFilePaths addObject:tempFilePath];
                if (fileName) {
                    [tempErrorFileNames addObject:fileName];
                }
            } else {
                successCount++;
            }
            
            if (failureCount + successCount < count) { //任务下载中
                
                double rate = (failureCount + successCount) / (double)count;
                if (isnan(rate) || isinf(rate)) {
                    rate = 0.00;
                }
                
                progress ? progress(rate) : nil;
                
            } else {  //下载完成
                
                if (failureCount == 0) { //全部下载成功
                    completionHandler ? completionHandler(YES, nil, nil) : nil;
                } else { //有下载失败
                    if (retry) {
                        
                        [self downloadForUrls:tempErrorURLs fileNames:tempErrorFileNames toFilePaths:tempErrorFilePaths downloadFailRetry:NO progress:progress completionHandler:completionHandler];
                        
                    } else {
                        completionHandler ? completionHandler(NO, tempErrorURLs, tempErrors) : nil;
                    }
                    
                }
            }
        }];
    }
}


@end
